package com.party.mobile.biz.photo;

import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.party.common.paging.Page;
import com.party.common.utils.DateUtils;
import com.party.common.utils.LangUtils;
import com.party.common.utils.StringUtils;
import com.party.core.exception.BusinessException;
import com.party.core.model.BaseModel;
import com.party.core.model.circle.Circle;
import com.party.core.model.circle.CircleMember;
import com.party.core.model.circle.CircleMemberTag;
import com.party.core.model.circle.CircleTag;
import com.party.core.model.commune.MomentType;
import com.party.core.model.file.ImgResource;
import com.party.core.model.member.Member;
import com.party.core.model.photo.*;
import com.party.core.service.circle.ICircleMemberService;
import com.party.core.service.circle.ICircleMemberTagService;
import com.party.core.service.circle.ICircleService;
import com.party.core.service.circle.ICircleTagService;
import com.party.core.service.file.IImgResourceService;
import com.party.core.service.member.IMemberService;
import com.party.core.service.photo.*;
import com.party.core.service.photo.biz.PhotosCountBizService;
import com.party.mobile.biz.member.CMomentBizService;
import com.party.mobile.web.dto.moment.CMomentSave;
import com.party.mobile.web.dto.photo.input.ImgUploadInput;
import com.party.mobile.web.dto.photo.output.ImgDetailOutput;
import com.party.mobile.web.dto.photo.output.ImgOutput;
import com.party.mobile.web.dto.photo.output.ImgTimeOutput;
import com.party.mobile.web.dto.photo.output.TagOutput;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Nullable;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: yangshoukun
 * @Date: 2018/6/5 10:00
 */
@Service
public class ImgBizService {

    private static Logger logger = LoggerFactory.getLogger(ImgBizService.class);

    @Autowired
    private IPhotosImgInfoService photosImgInfoService;

    @Autowired
    private IPhotosTagService photosTagService;

    @Autowired
    private IPhotosImgTagService photosImgTagService;

    @Autowired
    private PhotosCountBizService photosCountBizService;

    @Autowired
    private IPhotosMemberOperateDetailService photosMemberOperateDetailService;

    @Autowired
    private IPhotosMemberOperateService photosMemberOperateService;

    @Autowired
    private IPhotoProjectService photoProjectService;

    @Autowired
    private IMemberService memberService;

    @Autowired
    private ICircleMemberService circleMemberService;

    @Autowired
    private ICircleMemberTagService circleMemberTagService;

    @Autowired
    private ICircleTagService circleTagService;

    @Autowired
    private IImgResourceService imgResourceService;

    @Autowired
    private ICircleService circleService;
    @Autowired
    private CMomentBizService momentBizService;

    /**
     * 照片列表
     *
     * @param tagId
     * @param page
     * @return
     */
    public List<ImgOutput> list(String tagId, String photosId, Page page) {
        if (StringUtils.isEmpty(tagId)) {
            throw new BusinessException("分类id不能为空");
        }
        PhotosTag tag = photosTagService.get(tagId);

        List<PhotosImgInfoWithResource> list;
        PhotosImgInfo photosImgInfo = new PhotosImgInfo();
        Map<String, Object> params = Maps.newHashMap();
        params.put("showMore", true);
        if (null != tag && PhotosTag.ALBUM_HOT.equals(tag.getName())) {
            params.put("hot", true);
            photosImgInfo.setPhotosId(tag.getPhotosId());
            list = photosImgInfoService.listPageByTag(null, photosImgInfo, params, page);
            int size = 100;
            if (page.getTotalCount() > size) {
                page.setTotalCount(size);
            }
            int temp = (page.getPage() - 1) * page.getLimit() + list.size();
            if (temp > size) {
                list = list.subList(0, list.size() - (temp - size));
            }
        } else {
            photosImgInfo.setPhotosId(photosId);
            list = photosImgInfoService.listPageByTag(tagId, photosImgInfo, params, page);
        }
        return transfromList(list);
    }

    /**
     * 时间线的时间列表
     *
     * @param tagId
     * @param page
     * @return
     */
    public List<ImgTimeOutput> time(String tagId, Page page) {
        if (StringUtils.isEmpty(tagId)) {
            throw new BusinessException("相册分类id不能为空");
        }

        PhotosTag tag = photosTagService.get(tagId);
        List<Map<String, String>> timeLine = photosImgInfoService.listTimeLine(tag.getPhotosId(), page);
        if (CollectionUtils.isEmpty(timeLine)) {
            return null;
        }

        Map<String, Object> params = Maps.newHashMap();
        params.put("showMore", true);

        List<ImgTimeOutput> outputs = LangUtils.transform(timeLine, map -> {
            ImgTimeOutput output = new ImgTimeOutput();
            // 处理时间显示
            String takeTime = map.get("take_time");
            String dateStr = takeTime.substring(0, takeTime.length() - 3);
            String blank = " ";
            String hourStr = takeTime.substring(takeTime.length() - 2);
            String minuteStr = ":00";

            String hourStrEnd = new DecimalFormat("00").format(Long.valueOf(hourStr) + 1);

            String showTime = dateStr.concat(blank).concat(hourStr).concat(minuteStr);
            showTime = showTime.concat(" - ").concat(dateStr).concat(blank).concat(hourStrEnd).concat(minuteStr);

            output.setTime(showTime);

            List<PhotosImgInfoWithResource> imgInfos = photosImgInfoService.listTimeLineData(takeTime, tag.getPhotosId(), params, null);

//            List<ImgOutput> imgs = LangUtils.transform(imgInfos, info -> ImgOutput.transform(info));

            output.setImgs(null);
            output.setImgNum(imgInfos.size());
            return output;
        });
        return outputs;
    }

    /**
     * 某个时间线的数据
     *
     * @param time: '2018-06-08 12:00 - 2018-06-08 13:00'
     * @param page
     * @return
     */
    public List<ImgOutput> timeData(String photoId, String time, Page page) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("showMore", true);
        final String format = "2018-06-08 12";
        String takeTime = time.substring(0, format.length());
        List<PhotosImgInfoWithResource> imgInfos = photosImgInfoService.listTimeLineData(takeTime, photoId, params, page);
        return transfromList(imgInfos);
    }

    /**
     * 验证该用户是否可以操作相册
     *
     * @return
     */
    public boolean verify(String memberId, String photosId) {
        if (StringUtils.isEmpty(memberId)) {
            throw new BusinessException("用户id不能为空");
        }
        if (StringUtils.isEmpty(photosId)) {
            throw new BusinessException("相册id不能为空");
        }

        PhotoProject photoProject = photoProjectService.get(photosId);

        // 全部允许
        if (PhotoProject.OPERATE_PERMISSION_ALL_YES.equals(photoProject.getOperatePermission())) {
            return true;
        }
        // 全部不允许
        if (PhotoProject.OPERATE_PERMISSION_ALL_NO.equals(photoProject.getOperatePermission())) {
            return false;
        }
        // 圈子内成员允许
        if (PhotoProject.OPERATE_PERMISSION_CIRCLE_ALL.equals(photoProject.getOperatePermission())) {
            CircleMember circleMember = circleMemberService.getUnique(new CircleMember(photoProject.getCircleId(), memberId));
            if (null == circleMember) {
                return false;
            }
            return true;
        }
        // 圈子内标签成员允许
        if (PhotoProject.OPERATE_PERMISSION_CIRCLE_TYPE.equals(photoProject.getOperatePermission())) {
            // 首先用户必须是圈子会员
            CircleMember circleMember = circleMemberService.getUnique(new CircleMember(photoProject.getCircleId(), memberId));
            if (null == circleMember) {
                return false;
            }

            Circle circle = circleService.get(photoProject.getCircleId());
            boolean noTypeIsOperate = circle.getNoTypeIsOperate() == 1 ? true : false;

            CircleMemberTag param = new CircleMemberTag();
            param.setCircle(photoProject.getCircleId());
            param.setDelFlag(BaseModel.DEL_FLAG_NORMAL);
            param.setMember(memberId);
            List<CircleMemberTag> circleMemberTags = circleMemberTagService.listPage(param, null);

            if (CollectionUtils.isEmpty(circleMemberTags)) {
                // 如果这个用户没有圈子分类标签, 但是圈子对'其他'是可操作的, 就返回true
                if (noTypeIsOperate) {
                    return true;
                } else {
                    return false;
                }
            }

            CircleTag param_tag = new CircleTag();
            param_tag.setCircle(photoProject.getCircleId());
            param_tag.setDelFlag(BaseModel.DEL_FLAG_NORMAL);
            param_tag.setIsOperate(CircleTag.OPERATE_YES);
            List<CircleTag> circleTags = circleTagService.listPage(param_tag, null);

            if (CollectionUtils.isEmpty(circleTags)) {
                return false;
            }

            boolean isOK = false;
            out:
            for (int i = 0, j = circleMemberTags.size(); i < j; i++) {
                CircleMemberTag circleMemberTag = circleMemberTags.get(i);

                for (int m = 0, n = circleTags.size(); m < n; m++) {
                    CircleTag circleTag = circleTags.get(m);

                    if (circleMemberTag.getTag().equals(circleTag.getId())) {
                        isOK = true;
                        break out;
                    }
                }
            }

            return isOK;
        }

        return false;
    }

    /**
     * 图片上传
     *
     * @param input
     * @return
     */
    @Transactional
    public Map<String,Object> upload(ImgUploadInput input, Member member) {
        Map<String,Object> imgMap = Maps.newHashMap();
        List<ImgUploadInput.ImgInput> imgs = Lists.newArrayList();
        List<String> imgOrgs = new ArrayList<>();
        List<ImgResource> imgResources = JSONObject.parseArray(input.getImgStrs(), ImgResource.class);
        for (ImgResource img : imgResources) {
            String resourceId = imgResourceService.insert(img);

            String iAppid = img.getPath().split("/")[1];
            String iBucket = img.getPath().split("/")[2];
            String cdnUrl = "http://" + iBucket + "-" + iAppid + ".image.myqcloud.com" + img.getPath().replace("/" + iAppid + "/" + iBucket, "");

            ImgUploadInput.ImgInput imgInput = input.new ImgInput();
            imgInput.setResourceId(resourceId);
            imgInput.setUrl(cdnUrl);
            imgInput.setPath(img.getPath());
            imgs.add(imgInput);
            imgOrgs.add(cdnUrl.replace(",",""));
        }

        boolean hasTag;
        if (StringUtils.isEmpty(input.getTagId())) {
            hasTag = false;
        } else {
            PhotosTag photosTag = photosTagService.get(input.getTagId());
            if (null == photosTag) {
                hasTag = false;
            } else {
                // 时间线和热门, 还有全部不用添加关系维护
                if (PhotosTag.ALBUM_HOT.equals(photosTag.getName()) || PhotosTag.ALBUM_TIME_LINE.equals(photosTag.getName())) {
                    hasTag = false;
                } else {
                    hasTag = true;
                }
            }
        }

        StringBuilder operateId = new StringBuilder("");
        try {
            operateId.append(photosCountBizService.countOperate(input.getPhotosId(), member.getId()));
            // 相册的操作次数
            PhotoProject photoProject = photoProjectService.get(input.getPhotosId());
            photoProject.setOperateNum(photoProject.getOperateNum() + 1);
            photoProjectService.update(photoProject);
        } catch (Exception e) {
            logger.error("上传图片时, 统计用户相册操作数异常", e);
        }

        imgs.forEach(img -> {
            PhotosImgInfo photosImgInfo = new PhotosImgInfo();
            photosImgInfo.setUrl(img.getUrl());
            photosImgInfo.setPhotosId(input.getPhotosId());

            // 先查看该图片链接是否已经存在, 如果存在就不用添加了
            List<PhotosImgInfo> list = photosImgInfoService.list(photosImgInfo);
            if (CollectionUtils.isEmpty(list)) {
                ImgResource imgResource = imgResourceService.get(img.getResourceId());
                if (null != imgResource) {
                    photosImgInfo.setTakeTime(imgResource.getShootTime());
                }

                photosImgInfo.setOperateNum(0);
                photosImgInfo.setViewNum(0);
                photosImgInfo.setCreateBy(member.getId());
                photosImgInfo.setUpdateBy(member.getId());
                photosImgInfo.setImgResourceId(img.getResourceId());
                photosImgInfo.setCanDel(PhotosImgInfo.CAN_DEL);
                String imgId = photosImgInfoService.insert(photosImgInfo);

                imgMap.put(img.getPath(), imgId);

                try {
                    photosCountBizService.countOperateDetail(input.getPhotosId(), member.getId(), operateId.toString(), imgId, OperateEnum.OPERATE_UPLOAD_IMG);
                } catch (Exception e) {
                    logger.error("上传图片时, 用户操作详情异常", e);
                }

                try {
                    photosCountBizService.countImgOperate(imgId);
                } catch (Exception e) {
                    logger.error("上传图片时, 图片操作量统计异常", e);
                }
            } else {
                photosImgInfo = list.get(0);
            }

            // 需要维护分类关系就添加对应关系
            if (hasTag) {
                PhotosImgTag photoImgTag = new PhotosImgTag();
                photoImgTag.setImgId(photosImgInfo.getId());
                photoImgTag.setTagId(input.getTagId());

                // 先查看图片与相册的关系是否已经存在, 有就不用添加了
                List<PhotosImgTag> relationList = photosImgTagService.list(photoImgTag);
                if (CollectionUtils.isEmpty(relationList)) {
                    photoImgTag.setPhotosId(input.getPhotosId());
                    photosImgTagService.insert(photoImgTag);
                }
            }
        });

        try {
            photosCountBizService.CountImgNum(input.getPhotosId(), new String[]{input.getTagId()});
        } catch (Exception e) {
            logger.error("添加照片添加各照片数量冗余数异常", e);
        }
        try {
            PhotoProject photoProject = photoProjectService.get(input.getPhotosId());
            CMomentSave cMomentSave = new CMomentSave();
            Map<String, Object> mmp = Maps.newHashMap();
            String[] picArr = imgOrgs.toArray(new String[imgOrgs.size()]);
            if(picArr.length>9){
                picArr = Arrays.copyOf(picArr, 9);
            }
            mmp.put("photoProjectId",input.getPhotosId());
            mmp.put("pics",String.join("|", picArr));
            mmp.put("createBy",photoProject.getMemberId());
            mmp.put("photoTitle",photoProject.getName());
            mmp.put("memberId",member.getId());
            cMomentSave.setDatas(mmp);
            cMomentSave.setPartnerId(photoProject.getMemberId());
            cMomentSave.setBizId(input.getPhotosId());
            cMomentSave.setMomentType(MomentType.PHOTO_PUBLISH.getCode());
            momentBizService.businessSaveCMoment(cMomentSave);
        }catch (Exception e){
            logger.error("上传相册同时发布动态--失败");
        }

        return imgMap;
    }

    /**
     * 移动图片
     *
     * @param tagId:  图片本来的分类(点开图片的地方)
     * @param imgIds: 图片的id, 用英文逗号隔开
     * @param tagIds: 要移动到的分类id, 用英文逗号隔开
     * @return
     */
    @Transactional
    public boolean move(String tagId, String imgIds, String tagIds, Member member) {
        if (StringUtils.isEmpty(imgIds)) {
            throw new BusinessException("照片id不能为空");
        }
        if (StringUtils.isEmpty(tagIds)) {
            throw new BusinessException("相册id不能为空");
        }

        List<String> oldTags = Lists.newArrayList();
        // 先删除以前选中的分类的关系
        if (StringUtils.isNotEmpty(tagId)) {
            PhotosImgTag param = new PhotosImgTag();
            param.setImgId(imgIds);
            param.setTagId(tagId);
            List<PhotosImgTag> list = photosImgTagService.listFindInSet(param);
            if (CollectionUtils.isNotEmpty(list)) {
                list.forEach(relation -> {
                    PhotosTag tag = photosTagService.get(relation.getTagId());
                    // 精选不能删除
                    if (PhotosTag.ALBUM_CHOICENESS.equals(tag.getName())) {
                        return;
                    }
                    oldTags.add(relation.getTagId());
                    photosImgTagService.delete(relation.getId());
                });
            }
        }

        String[] tagArray = tagIds.split(",");
        String[] imgArray = imgIds.split(",");

        String photosId = photosImgInfoService.get(imgArray[0]).getPhotosId();

        StringBuilder operateId = new StringBuilder("");
        try {
            operateId.append(photosCountBizService.countOperate(photosId, member.getId()));
            // 相册的操作次数
            PhotoProject photoProject = photoProjectService.get(photosId);
            photoProject.setOperateNum(photoProject.getOperateNum() + 1);
            photoProjectService.update(photoProject);
        } catch (Exception e) {
            logger.error("移动图片时, 统计用户相册操作数异常", e);
        }

        // 处理-每次操作, 只统计一次操作量
        Map<String, String> tempMap = Maps.newHashMap();

        for (int i = 0; i < imgArray.length; i++) {
            String imgId = imgArray[i];
            PhotosImgInfo photosImgInfo = photosImgInfoService.get(imgId);

            for (int j = 0; j < tagArray.length; j++) {
                String _tagId = tagArray[j];

                if ("0".equals(_tagId)) {
                    moveCount(photosId, member, operateId.toString(), imgId, tempMap);
                    continue;
                }
                PhotosTag _photosTag = photosTagService.get(_tagId);
                // 时间线和热门不需要添加关系
                if (PhotosTag.ALBUM_HOT.equals(_photosTag.getName()) || PhotosTag.ALBUM_TIME_LINE.equals(_photosTag.getName())) {
                    moveCount(photosId, member, operateId.toString(), imgId, tempMap);
                    continue;
                }

                PhotosImgTag photosImgTag = new PhotosImgTag();
                photosImgTag.setImgId(imgId);
                photosImgTag.setTagId(_tagId);
                photosImgTag.setPhotosId(photosImgInfo.getPhotosId());

                // 如果关系已经存在, 就不添加了
                List<PhotosImgTag> tempList = photosImgTagService.list(photosImgTag);
                if (CollectionUtils.isEmpty(tempList)) {
                    photosImgTag.setCreateBy(member.getId());
                    photosImgTag.setUpdateBy(member.getId());

                    photosImgTagService.insert(photosImgTag);

                    moveCount(photosId, member, operateId.toString(), imgId, tempMap);
                }
            }
        }

        try {
            String[] tags = new String[tagArray.length + oldTags.size()];
            System.arraycopy(tagArray, 0, tags, 0, tagArray.length);
            String[] olds = new String[oldTags.size()];
            olds = oldTags.toArray(olds);
            System.arraycopy(olds, 0, tags, tagArray.length, olds.length);
            photosCountBizService.CountImgNum(photosId, tags);
        } catch (Exception e) {
            logger.error("移动照片时添加各照片数量冗余数异常", e);
        }

        return true;
    }

    private void moveCount(String photosId, Member member, String operateId, String imgId, Map<String, String> tempMap) {
        // 如果是全部, 就直接添加操作记录
        try {
            photosCountBizService.countOperateDetail(photosId, member.getId(), operateId, imgId, OperateEnum.OPERATE_MOVE_TAG);
        } catch (Exception e) {
            logger.error("移动图片时, 用户操作详情异常", e);
        }
        try {
            // 处理-每次操作, 只统计一次操作量
            if (StringUtils.isEmpty(tempMap.get(imgId))) {
                photosCountBizService.countImgOperate(imgId);
                tempMap.put(imgId, imgId);
            }
        } catch (Exception e) {
            logger.error("移动图片时, 图片操作量统计异常", e);
        }
    }

    /**
     * 图片详情
     *
     * @param id:    图片id
     * @param tagId: 从哪个分类进入图片详情的
     * @return
     */
    public ImgDetailOutput detail(String id, String tagId, Integer isCount) {
        if (StringUtils.isEmpty(id)) {
            throw new BusinessException("图片id不能为空");
        }
        PhotosImgInfo imgInfo = photosImgInfoService.get(id);
        if (null == imgInfo) {
            throw new BusinessException("图片不能为空");
        }

        // 点击图片详情, 图片浏览量增加1
        if (null == isCount) {
            isCount = 1;
        }
        if (1 == isCount) {
            try {
                imgInfo.setViewNum(imgInfo.getViewNum() + 1);
                photosImgInfoService.update(imgInfo);
            } catch (Exception e) {
                logger.error("点击图片详情, 增加图片浏览量异常", e);
            }
        }

        ImgDetailOutput output = ImgDetailOutput.transform(imgInfo);

        output.setTagId(tagId);

        // 找到分类
        List<PhotosTag> tagList = photosTagService.listByImgId(new PhotosTag(), id);
        List<TagOutput> tags = LangUtils.transform(tagList, tag -> TagOutput.transform(tag));

        output.setTags(tags);

        // 最后操作时间
        PhotosMemberOperateDetail param = new PhotosMemberOperateDetail();
        param.setImgId(id);
        PhotosMemberOperateDetail photosMemberOperateDetail = photosMemberOperateDetailService.getLastTimeOperate(param);

        output.setLastTime(null == photosMemberOperateDetail ? "" : DateUtils.formatDateTime(photosMemberOperateDetail.getCreateDate()));

        // 最后操作人
        Member member = memberService.get(null == photosMemberOperateDetail ? "0" : photosMemberOperateDetail.getMemberId());

        output.setLastMember(null == member ? "" : member.getRealname());

        // 最后操作人的操作次数
        PhotosMemberOperate photosMemberOperate = null == photosMemberOperateDetail ? null : photosMemberOperateService.get(photosMemberOperateDetail.getOperateId());

        output.setMemberOperateNum(null == photosMemberOperate ? null : photosMemberOperate.getOperateNum());

        // 机构id
        PhotoProject photoProject = photoProjectService.get(imgInfo.getPhotosId());

        output.setPartnerId(null == photoProject ? null : photoProject.getMemberId());

        return output;
    }

    /**
     * 渲染输出
     *
     * @param resourceList
     * @return
     */
    private List<ImgOutput> transfromList(List<PhotosImgInfoWithResource> resourceList) {
        List<ImgOutput> outputs = LangUtils.transform(resourceList, new Function<PhotosImgInfoWithResource, ImgOutput>() {
            @Nullable
            @Override
            public ImgOutput apply(@Nullable PhotosImgInfoWithResource input) {
                ImgOutput transform = transfromObject(input);
                return transform;
            }
        });
        return outputs;
    }

    public ImgOutput transfromObject(PhotosImgInfo input) {
        ImgOutput transform = ImgOutput.transform(input);
        if (StringUtils.isNotEmpty(input.getTagName())) {
            List<TagOutput> tagOutputs = Lists.newArrayList();
            List<String> tags = Arrays.asList(input.getTagName().split(","));
            for (String tag : tags) {
                String[] split = tag.split("-");
                TagOutput tagOutput = new TagOutput();
                tagOutput.setName(split[0]);
                tagOutput.setId(split[1]);
                tagOutputs.add(tagOutput);
            }
            transform.setTags(tagOutputs);
        }
        if (null != input.getLastTime()) {
            transform.setLastTime(DateUtils.formatDateTime(input.getLastTime()));
        }
        transform.setMemberOperateNum(input.getMemberOperateNum());
        return transform;
    }

    public Integer initOperate() {
        Integer count = 0;
        List<String> imgList = photosMemberOperateDetailService.hasOperatedImg();
        for (String imgId : imgList) {
            try {
                PhotosMemberOperateDetail param = new PhotosMemberOperateDetail();
                param.setImgId(imgId);
                PhotosMemberOperateDetail photosMemberOperateDetail = photosMemberOperateDetailService.getLastTimeOperate(param);
                if (null != photosMemberOperateDetail) {
                    PhotosImgInfo photosImgInfo = photosImgInfoService.get(imgId);
                    photosImgInfo.setLastTime(photosMemberOperateDetail.getCreateDate());
                    photosImgInfo.setLastMember(photosMemberOperateDetail.getMemberId());
                    photosImgInfo.setOperateId(photosMemberOperateDetail.getOperateId());
                    photosImgInfoService.update(photosImgInfo);

                    count++;
                }
            } catch (Exception e) {
                logger.error("获取照片的操作详情异常", e);
                continue;
            }
        }
        return count;
    }

    /**
     * 删除照片
     *
     * @param imgIds: 图片id
     */
    @Transactional
    public void delete(String imgIds) {
        if (StringUtils.isEmpty(imgIds)) {
            throw new BusinessException("照片id不能为空");
        }

        // 再删除图片信息
        String[] imgArray = imgIds.split(",");
        for (String s : imgArray) {
            PhotosImgInfo photosImgInfo = photosImgInfoService.get(s);
            imgResourceService.delete(photosImgInfo.getImgResourceId());
            photosImgInfoService.delete(s);
        }

        // 删除照片和分类的关系
        PhotosImgTag param = new PhotosImgTag();
        param.setImgId(imgIds);
        List<PhotosImgTag> list = photosImgTagService.listFindInSet(param);
        if (CollectionUtils.isNotEmpty(list)) {
            list.forEach(photosImgTag -> {
                photosImgTagService.delete(photosImgTag.getId());

                try {
                    photosCountBizService.CountImgNum(photosImgTag.getPhotosId(), new String[]{photosImgTag.getTagId()});
                } catch (Exception e) {
                    logger.error("删除照片添加各照片数量冗余数异常", e);
                }
            });
        }
    }
}
